package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.converter.PermissionConverter;
import com.youlai.system.mapper.PermissionMapper;
import com.youlai.system.model.entity.Permission;
import com.youlai.system.model.form.PermissionForm;
import com.youlai.system.model.vo.PermissionVO;
import com.youlai.system.service.PermissionService;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Data
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    private final PermissionMapper permissionMapper;
    private final PermissionConverter permissionConverter;
    @Override
    public List<PermissionVO> listPermissions() {



        List<PermissionVO> list = permissionMapper.list();
        return list;
    }

    @Override
    public Integer savePermission(PermissionForm formData) {
        Permission permission = permissionConverter.formEntity(formData);

        boolean result = this.saveOrUpdate(permission);
        Assert.isTrue(result,"权限保存失败");
        return permission.getId();
    }

    @Override
    public boolean deleteByIds(String ids) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<Permission>()
                .eq("id",ids);
        return this.remove(wrapper);
    }

    @Override
    public PermissionForm getPermissionForm(Long id) {
        Permission entity = this.getById(id);
        return permissionConverter.entityForm(entity);
    }
}
